package com.commonlibrary.httpframe;


import android.app.ProgressDialog;
import android.content.Context;


import com.commonlibrary.utils.CompressUtils;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import javax.net.ssl.SSLHandshakeException;

import io.reactivex.observers.DisposableObserver;
import okhttp3.ResponseBody;
import retrofit2.HttpException;

/**
 * 返回结果统一处理
 */
public class HttpResultObserver extends DisposableObserver<ResponseBody>
        implements ListenerProgressCancel {

    private ProgressDialog progressDialog;
    private ListenerHttpResult listenerResult;
    private String apiTag = null;


    public HttpResultObserver(Context context, ListenerHttpResult listenerResult) {
        this.listenerResult = listenerResult;
        progressDialog = new ProgressDialog(context);
    }


    @Override
    protected void onStart() {
        showProgressDialog();
    }

    @Override
    public void onNext(ResponseBody body) {
        try {
            final String result = CompressUtils.decompress(body.byteStream());
            //TODO 根据自家后台返回格式处理
            listenerResult.onSuccess(result);

        } catch (Exception e) {
            e.printStackTrace();
            listenerResult.onFail(e.getMessage());
        } finally {
            if (body != null) {
                body.close();
            }
        }
    }


    @Override
    public void onComplete() {
        dismissProgressDialog();
        progressDialog = null;
    }

    @Override
    public void onError(Throwable e) {
        try {
            String errorMsg;
            if (e instanceof SocketTimeoutException) {//请求超时
                errorMsg = "网络请求超时";
            } else if (e instanceof ConnectException) {//网络连接超时
                errorMsg = "网络连接超时";
            } else if (e instanceof SSLHandshakeException) {//安全证书异常
                errorMsg = "安全证书异常";
            } else if (e instanceof HttpException) {
                int code = ((HttpException) e).code();
                if (code == 504) {
                    errorMsg = "网络异常，请检查您的网络状态";
                } else if (code == 404) {
                    errorMsg = "请求的地址不存在";
                } else {
                    errorMsg = "请求失败:" + e.getMessage();
                }
            } else if (e instanceof UnknownHostException) {
                errorMsg = "域名解析失败";
            } else {
                errorMsg = "error:" + e.getMessage();
            }
            listenerResult.onFail(errorMsg);
        } catch (Exception e2) {
            e2.printStackTrace();
            listenerResult.onFail("Exception:" + e2.getMessage());
        } finally {
            dismissProgressDialog();
            progressDialog = null;
        }

    }


    private void showProgressDialog() {
        if (progressDialog != null) {
            progressDialog.show();
        }
    }

    private void dismissProgressDialog() {
        if (progressDialog != null) {
            progressDialog.hide();
            onCancelProgress();
        }
    }

    /**
     * 取消ProgressDialog的时候，取消对observable的订阅，同时也取消了http请求
     */
    @Override
    public void onCancelProgress() {
        if (!this.isDisposed()) {
            this.dispose();
        }
    }
}
